package com.time.hellotime.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.time.hellotime.R;


/**
 * 登录密码自定义布局
 * Author: Created by deng on 2016/8/18.
 * E-mail: tx16bing@163.com
 */
public class PwdLayout extends FrameLayout {

    private EditText etPassword;
    private ImageView ivClear, ivEye;

    private boolean loginPswEye;
    private String hintText;
    private boolean isUser;

    public PwdLayout(Context context) {
        super(context);
    }

    public PwdLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_pwd, this, false);
        addView(view);
        initView(view);
    }

    private void initView(final View view) {
//        final ImageView ivIcon = (ImageView) view.findViewById(R.id.iv_icon);
        etPassword = (EditText) view.findViewById(R.id.et_password);

        ivClear = (ImageView) view.findViewById(R.id.iv_clear);
        ivEye = (ImageView) view.findViewById(R.id.iv_eye);
        ivClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                etPassword.setText("");
            }
        });

        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etPassword.getText().length() == 0) {
                    ivClear.setVisibility(View.GONE);
                    ivEye.setVisibility(View.GONE);
//                    ivIcon.setImageResource(R.drawable.login_password_unpressed);
                } else {
//                    ivIcon.setImageResource(R.drawable.login_password_pressed);
                    if (isUser){
                        ivClear.setVisibility(GONE);
                        ivEye.setVisibility(VISIBLE);
                    }else {
                        ivClear.setVisibility(VISIBLE);
                        ivEye.setVisibility(GONE);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etPassword.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
//                    ivIcon.setImageResource(R.drawable.login_password_pressed);
                    if (!TextUtils.isEmpty(etPassword.getText())) {
                        if (isUser){
                            ivClear.setVisibility(GONE);
                            ivEye.setVisibility(VISIBLE);
                        }else {
                            ivClear.setVisibility(VISIBLE);
                            ivEye.setVisibility(GONE);
                        }
                    }
                } else {
//                    ivIcon.setImageResource(R.drawable.login_password_unpressed);
                    ivClear.setVisibility(View.GONE);
                    ivEye.setVisibility(View.GONE);
                }
            }
        });

        ivEye.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!loginPswEye){
                    ivEye.setImageResource(R.drawable.iv_psd_not_show);
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    loginPswEye = true;
                }else{
                    ivEye.setImageResource(R.drawable.iv_psd_show);
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    loginPswEye = false;
                }
                etPassword.setSelection(etPassword.getText().toString().trim().length());
            }
        });
    }

    public String getPassword() {
        return etPassword.getText().toString().trim();
    }

    public void setHintText(int hintText) {
        etPassword.setHint(hintText);
        invalidate();
    }

    public EditText getEtPassword() {
        return etPassword;
    }

    public void setUser(boolean user) {
        isUser = user;
    }
}
